package qldoanra.baocao.theothoigian;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import utils.DateUtils;
import utils.UtilsOnScopes;
import dbutil.ConnectDB;

public class BaoCao {
	private String error;
	private java.util.Date  starDate;
	private java.util.Date endDate;
	private String pattern;
	private String path="./emptyBieuMau.xhtml";
	private List<TableDisplay> list;
	
	public String tables() {
		List<TableDisplay> list = new ArrayList<TableDisplay>();
		Date starDateSQL = DateUtils.utilDateToSqlDate(this.getStarDate());
		Date endDateSQL = DateUtils.utilDateToSqlDate(this.getEndDate());
		String SQL = "select *,(D.ngayve +7) as ngayquidinhnop from hochieu A inner join canhan B on A.cmnd = B.cmnd "+
		              " inner join dsnguoidi C on C.sohc = A.sohc " +
		              " left join doanra D on D.madoan =C.iddoanra " +
		              " left join mucdich E on D.idmucdich = E.id " +
		              " inner join quyetdinh F on D.idquyetdinh = F.id " +
		              " inner join (select madoan,sohc,max(G.ngaythaotac) as ngaytrathucte,min(G.ngaythaotac) as ngaynhanhc"
		              +             " from nhatkytranhanhc G group by G.madoan,G.sohc) K "+
		              " on (a.sohc = K.sohc and K.madoan = d.madoan)" +
		              " WHERE D.ngaydi BETWEEN ' " + starDateSQL + "' AND '"
					  + endDateSQL + "' ORDER BY  C.tennguoidi DESC";
		System.out.println(SQL);
		ArrayList<HashMap<String, Object>> array = ConnectDB
				.getDataTypeList(SQL);
		try {
			if (array.size() > 0) {
				HashMap<String, Object> hash = new HashMap<String, Object>();
				int stt = 0;
				for (int i = 0; i < array.size(); i++) {
					stt = i + 1;
					hash = (HashMap<String, Object>) array.get(i);
					TableDisplay table = new TableDisplay();
					table.setStt(String.valueOf(stt));
					table.setChiphi(hash.get("chiphi")==null?"":hash.get("chiphi").toString());
					table.setChucvu(hash.get("chucvu")== null?"":hash.get("chucvu").toString());
					table.setCmnd(hash.get("cmnd")== null?"":hash.get("cmnd").toString());
					table.setCoquan(hash.get("coquan")==null?"":hash.get("coquan").toString());
					table.setDonvimoi(hash.get("donvimoi")==null?"":hash.get("donvimoi").toString());
					table.setGioitinh(hash.get("gioitinh")==null?"":hash.get("gioitinh").toString());
					table.setLinhvuc(hash.get("linhvuc")==null?"":hash.get("linhvuc").toString());
					table.setLoaidoan(hash.get("loaidoan")==null?"":hash.get("loaidoan").toString());
					table.setMucdich(hash.get("mucdich")==null?"":hash.get("mucdich").toString());
					table.setNamsinh(hash.get("namsinh")==null?"":hash.get("namsinh").toString());
					table.setNgaydi(hash.get("ngaydi")==null?"":hash.get("ngaydi").toString());
					table.setNgayve(hash.get("ngayve")==null?"":hash.get("ngayve").toString());
					
					String ngaytrathucte = DateUtils.formatDate(hash.get("ngaytrathucte")==null?"":hash.get("ngaytrathucte").toString());
					String ngaygiao = DateUtils.formatDate(hash.get("ngaynhanhc")==null?"":hash.get("ngaynhanhc").toString());
					if(ngaytrathucte.equalsIgnoreCase(ngaygiao)){
						table.setNgaygiaohc(ngaygiao);
						table.setTrangthainop("Chưa nộp");
					}else{
						table.setTrangthainop("Đã nộp");
						table.setNgaygiaohc(ngaygiao);
						table.setNgaynopthucte(ngaytrathucte);
					}
					table.setNgaykyqd(DateUtils.formatDate(hash.get("ngaykyqd")== null?"":hash.get("ngaykyqd").toString()));
					table.setNgayquydinhnop( DateUtils.formatDate(hash.get("ngayquidinhnop")== null?"":hash.get("ngayquidinhnop").toString()));
					table.setNguoikyqd(hash.get("nguoikyqd")==null?"":hash.get("nguoikyqd").toString());
					table.setNuocden(hash.get("nuocden")==null?"":hash.get("nuocden").toString());
					table.setSohc(hash.get("sohc")==null?"":hash.get("sohc").toString());
					table.setSoqd(hash.get("soqd")== null?"":hash.get("soqd").toString());
					table.setTen(hash.get("ten")==null?"":hash.get("ten").toString());
					table.setTrangthai(hash.get("trangthai")==null?"":hash.get("trangthai").toString());
					table.setTrangthaidi(hash.get("trangthaidi")==null?"":hash.get("trangthaidi").toString());
					
					if(hash.get("dangvien")!=null &&hash.get("dangvien").toString().equals("true")){
						table.setPathImgDangVien("/qldoanra/img/Ok.gif");
					}
					if(hash.get("baocaokq")!=null &&hash.get("baocaokq").toString().equals("true")){
						table.setPathImgBaoCaoKQ("/qldoanra/img/Ok.gif");
					}
					table.setLoaihc(hash.get("loaihc")==null?"":hash.get("loaihc").toString());
					list.add(table);
				}
			}else{
				list = new ArrayList<TableDisplay>();
			}
			this.setList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	
	/**************************************
	 * Method name		: view
	 * Return type		: String
	 * Description		:xem du lieu
	 * Created date		: Sep 4, 2008
	 * Author			: Honglk
	 **************************************/
	public String view() {
		this.setError("");
		if (this.checkValue(this.starDate, this.endDate) == false) {
			return "";
		} else {
			this.tables();
			if (this.list.size() <= 0) {
				this.setError("Không có dữ liệu !");

			} else {
				int model = Integer.parseInt(this.pattern);
				switch (model) {
				case -1:
					this.setPath("./bieumauAll.xhtml");
					break;
				case 3:
					this.setPath("./bieumau3cot.xhtml");
					break;
				case 5:
					this.setPath("./bieumau5cot.xhtml");
					break;
				case 8:
					this.setPath("./bieumau8cot.xhtml");
					break;
				case 10:
					this.setPath("./bieumau10cot.xhtml");
					break;
				case 15:
					this.setPath("./bieumau15cot.xhtml");
					break;
				default:
					this.setPath("");
					break;
				}
			}
		}
		return "okBaoCaoTheoThoigian";
	}
	
	/**************************************
	 * Method name		: checkValue
	 * Return type		: boolean
	 * Decription		:Kiem tra du lieu dau vao
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public boolean checkValue( java.util.Date star,java.util.Date  end){
		boolean result = true;
		if(star == null || star.equals("")){
			this.setError("Bạn chưa chọn ngày bắt đầu !");
			return result = false;
		}
		else if(end == null || end.equals("")){
			this.setError("Bạn chưa chọn ngày kết thúc !");
		  return result = false;
		}
		else if(star.compareTo(end) > 0){
			this.setError("Ngày kết thúc lớn hơn ngày bắt đầu !");
			return result = false;
			
		}
		return result;
	}
	
	public String reset() {
		UtilsOnScopes.getSession().removeAttribute("baoCao");
		this.setStarDate(null);
		this.setEndDate(null);
		return "okBaoCaoTheoThoigian";
	}
	
	public List<TableDisplay> getList() {
//		this.tables();
		return list;
	}

	public void setList(List<TableDisplay> list) {
		this.list = list;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public java.util.Date getStarDate() {
		return starDate;
		
	}

	public void setStarDate(java.util.Date starDate) {
		this.starDate = starDate;
	}

	public java.util.Date getEndDate() {
		return endDate;
	}

	public void setEndDate(java.util.Date endDate) {
		this.endDate = endDate;
	}
	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	public static void main(String[] args) {
		BaoCao bc = new BaoCao();
		System.out.println("");
	}

}
